package com.zeno.chinesecalendar;

import java.util.Map;
import java.util.TreeMap;

/**
 * @author wangzezhou
 * @date 2021/11/26
 */
public class BenderSelector {

    public static final String[] EIGHT_TRIGRAMS_POSITION = {"生门","休门","死门","开门","景门","惊门","伤门","杜门"};

    private static final Map<String,Integer> CELESTIAL_VALUE;
    private static final Map<String,Integer> TERRESTRIAL_VALUE;

    static {
        CELESTIAL_VALUE = new TreeMap<>();
        CELESTIAL_VALUE.put("甲",9);
        CELESTIAL_VALUE.put("乙",8);
        CELESTIAL_VALUE.put("丙",7);
        CELESTIAL_VALUE.put("丁",6);
        CELESTIAL_VALUE.put("戊",5);
        CELESTIAL_VALUE.put("己",9);
        CELESTIAL_VALUE.put("庚",8);
        CELESTIAL_VALUE.put("辛",7);
        CELESTIAL_VALUE.put("壬",6);
        CELESTIAL_VALUE.put("癸",5);

        TERRESTRIAL_VALUE = new TreeMap<>();
        TERRESTRIAL_VALUE.put("子",9);
        TERRESTRIAL_VALUE.put("丑",8);
        TERRESTRIAL_VALUE.put("寅",7);
        TERRESTRIAL_VALUE.put("卯",6);
        TERRESTRIAL_VALUE.put("辰",5);
        TERRESTRIAL_VALUE.put("巳",4);
        TERRESTRIAL_VALUE.put("午",9);
        TERRESTRIAL_VALUE.put("未",8);
        TERRESTRIAL_VALUE.put("申",7);
        TERRESTRIAL_VALUE.put("酉",6);
        TERRESTRIAL_VALUE.put("戌",5);
        TERRESTRIAL_VALUE.put("亥",4);
    }

    public static String getPosition(String yearSemBranch,String monthSemBranch,String daySemBranch,String hourSemBranch){

        String a = String.valueOf(yearSemBranch.charAt(0));
        int i = CELESTIAL_VALUE.get(a);
        i += TERRESTRIAL_VALUE.get(String.valueOf(yearSemBranch.charAt(1)));

        i += CELESTIAL_VALUE.get(String.valueOf(monthSemBranch.charAt(0)));
        i += TERRESTRIAL_VALUE.get(String.valueOf(monthSemBranch.charAt(1)));

        i += CELESTIAL_VALUE.get(String.valueOf(daySemBranch.charAt(0)));
        i += TERRESTRIAL_VALUE.get(String.valueOf((daySemBranch.charAt(1))));

        i+= CELESTIAL_VALUE.get(String.valueOf(hourSemBranch.charAt(0)));
        i+= TERRESTRIAL_VALUE.get(String.valueOf(hourSemBranch.charAt(1)));

        i = i%8;
        if (i == 0){
            i = 8;
        }
        return EIGHT_TRIGRAMS_POSITION[i-1];
    }
}
